<template>
  <div class="dashboard-container">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="150px" class="demo-ruleForm"
             label-position="left">
      <el-row>
        <el-row>
          <el-col :span="12">
            <div class="grid-content bg-purple">
              <el-form-item label="机构相册名称：" prop="album_name">
                <el-input v-model="ruleForm.album_name" placeholder="请输入机构相册名称"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="相册所属的类型：" prop="album_typeid">
              <div class="grid-content bg-purple">
                <el-select v-model="ruleForm.album_typeid" filterable placeholder="请选择类型">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="ruleForm.album_typeid=='1'">
          <el-col :span="12">
            <el-form-item label="相册所属的校区：" prop="album_campusid">
              <div class="grid-content bg-purple-light">
                <el-select v-model="ruleForm.album_campusid" filterable placeholder="请选择相册所属的校区">
                  <el-option v-for="item in ComList" :key="item.id" :label="item.campus_name" :value="item.id">
                  </el-option>
                </el-select>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="alt：" prop="album_alt">
              <el-input v-model="ruleForm.album_alt" placeholder="请输入alt"></el-input>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item label="图片链接：">
              <el-input v-model="ruleForm.album_jumpurl" placeholder="请输入链接"></el-input>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <div style="display: flex; width: 100%; margin:20px 0;">
        <span style="display: flex; width: 120px; align-items: center; justify-content: left;font-size: 14px;
    color: #606266;">课程图：</span>
        <el-upload ref="upload" action="#" :limit="1"
                   :http-request="submitUpload"
                   :file-list="dataForm.imgFileList"
                   list-type="picture-card"
                   :on-preview="handlePictureCardPreview"
                   :on-change="OnChange"
                   :on-remove="handleRemove"
                   :on-exceed="handleExceed"
                   accept="image/jpeg,image/png">
          <i class="el-icon-plus"></i>
          <div class="el-upload__tip" slot="tip">单张图片不超过500kb
          </div>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible" append-to-body>
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </div>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {campusComList, uploadImg, albumAdd} from "@/api/menu";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      imgUrl: [],
      dataForm: {
        imgFileList: []//绑定用户上传的图片List
      },
      dialogImageUrl: '',
      dialogVisible: false,
      ruleForm: {
        album_name: '',
        album_typeid: '',
        album_campusid: '',
        album_url: '',
        album_alt: '',
        album_jumpurl: '',
      },
      rules: {
        album_name: [
          {required: true, message: '请输入机构相册名称', trigger: 'blur'}
        ],
        album_typeid: [
          {required: true, message: '请选择相册所属的类型', trigger: 'change'}
        ],
        album_campusid: [
          {required: true, message: '请选择相册所属的校区', trigger: 'change'}
        ],
        album_alt: [
          {required: true, message: '请输入alt', trigger: 'blur'}
        ]
      },
      options: [{value: "", label: "请选择"},{
        value: '1',
        label: '校区相册'
      }, {
        value: '2',
        label: 'banner'
      }, {
        value: '3',
        label: '资质相册'
      }],
      fileList: [{
        name: 'logo.jpeg',
        url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
      }],
      ComList: []
    }
  },
  computed: {
    ...mapGetters([
      'schoolId'
    ]),
  },
  mounted() {
    this.getCampusComList()
  },
  methods: {
    /**
     * 图片上传
     */
    submitUpload() {
      let formData = new FormData(); //  用FormData存放上传文件
      this.dataForm.imgFileList.forEach(file => {
        formData.append('file', file.raw)
      })
      uploadImg(formData).then(response => {
        console.log(response);
        this.dialogImageUrl = response.data;
        this.ruleForm.album_url = response.data;
        console.log(this.ruleForm.album_url);
      })
    },
    /**
     * 图片预览
     */
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    /**
     * 图片大小控制
     */
    OnChange(file, fileList) {
      const isLt1M = file.size / 1024 / 1024 / 0.5 < 1
      if ( !isLt1M ) {
        this.$message.error('上传头像图片大小不能超过 500kb!');
        fileList.pop()
      }
      this.dataForm.imgFileList.push(file)
      this.hideUpload = fileList.length >= this.limit

    },
    /**
     * 图片删除
     */
    handleRemove(file, fileList) {
      if ( file.id ) {
        console.log('删除了已被上传过的图片')
        console.log(file.id)
        this.deleteImgFileList.push(file.id)
      }
      this.dataForm.imgFileList = fileList
      this.hideUpload = fileList.length >= this.limit
    },
    /**
     * 公共查询分类校区数据
     */
    getCampusComList() {
      campusComList({citylog_schoolid: this.schoolId}).then(response => {
        console.log(response);
        this.ComList = response.data
      }).catch(e => {
        e
      });
    },
    /**
     * 增加
     * @param formName
     */
    submitForm(formName) {
      this.$refs[ formName ].validate((valid) => {
        if ( valid ) {
          albumAdd(Object.assign(this.ruleForm, {album_schoolid: this.schoolId})).then(response => {
            this.listLoading = false;
            console.log(response);
            this.$message({message: '新增成功', type: 'success', duration: 1000});
            this.$router.push({path: '/photoList', query: {id: this.$route.query.id}});
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[ formName ].resetFields();
      this.dataForm.imgFileList = []
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`只能上传一张图片`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    }
  }
}
</script>
<style scoped>
.my /deep/ .el-radio__inner {
  width: 18px;
  height: 18px;
}

.my /deep/ .el-radio__inner::after {
  width: 6px;
  height: 6px;
}

.small-button /deep/ .el-upload-list {
  display: none;
}

.mbys-box-right /deep/ .el-button {
  margin-left: 0px !important;
  height: 100%;
  background-color: #1f2d3d;
  border-color: #1f2d3d;
  border-radius: 0px;
  white-space: inherit;
}

.mbys-box-right /deep/ .el-button span {
  display: block;
  width: 15px;
  font-weight: bold;
  line-height: 25px;
  word-wrap: break-word;
  word-break: break-all;
  position: relative;
}
</style>
<style lang="scss" scoped>
.el-icon-caret-left {
  position: absolute;
  top: 20%;
  left: -65px;
  font-size: 80px;
  color: #1f2d3d;
}

.mbys-box-right {
  position: absolute;
  top: 0px;
  right: 0px;
  height: 100%;
}

.div-button {
  width: 10%;
}

.div-button:nth-child(2) {
  margin-left: 100px;
}

.dashboard-box-auto, .dashboard-box-bottom {
  margin-top: 15px;
}

.dashboard-container ul {
  margin: 0px;
  padding: 0px;
}

.dashboard-container li {
  list-style: none;
}

.dashboard-box-right > ul {
  font-size: 0;
  width: 100%;
}

.dashboard-box-right > ul > li:nth-child(1) {
  display: block;
  width: 100%;
  margin: 0;
  border: 5px solid #cccccc;
}

.dashboard-box-right > ul > li:nth-child(2) {
  margin-left: 0;
}

.dashboard-box-right > ul > li {
  width: 22%;
  font-size: 0;
  margin-left: 4%;
  margin-top: 15px;
  border: 3px solid #cccccc;
  display: inline-block;
}

.dashboard-box-right > ul > li > img {
  width: 100%;
}

.el-checkbox.is-bordered {
  margin: 0;
}

.el-select {
  width: 100%;
}

.dashboard-box-top {
  display: flex;
}

.dashboard-box-left {
  width: 50%;
}

.dashboard-box-right {
  width: 26%;
  margin-left: 10%;
}

.el-radio {
  &-inner {
    width: 18px;
    height: 18px;
  }
}

.dashboard {
  &-container {
    margin: 0 15px;
    padding: 30px;
    position: relative;
    background-color: #fff;
  }

  &-text {
    font-size: 16px;
  }
}
</style>
